/**
 * 
 * CONTROLLER of resizable pane...
 * 
 * @Ning
 * 
 * 2010-4-7
 * 
 * 
 * 
 * */
package com.components.pane.resizable
{
	import com.components.button.MCButton;
	
	import core.section.Controller;
	
	import datastream.recordset.RecordSet;
	
	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import mx.core.UIComponent;
	import mx.events.ResizeEvent;
	import mx.managers.CursorManager;
	
	[Event(name="pane_resize",type="com.components.pane.resizable.ResizePaneEvents")]
	
	public class ResizablePane extends Controller
	{
		//START...
		
		
		//PROPERTY...
		
		private var view:ResizablePaneView;
		
		private var cursors:Array;
		
		private var weight:Number;
		
		private var activeBar:MCButton;
		
		private var oriRect:Rectangle;
		
		private var activePoint:Point;
		
		private var resizing:Boolean;
		
		//CONSTRUCTOR...
		
		/**
		 * 
		 * 
		 * tlCursor:Class,
		   tCursor:Class,
		   trCursor:Class,
		   lCursor:Class,
		   rCursor:Class,
		   blCursor:Class,
		   bCursor:Class,
		   brCursor:Class
		 * 
		 * 
		 * 
		 * */
		
		public function ResizablePane(target:DisplayObject,cursors:Array,weight:Number = 4)
		{
			super(target);
			
			this.cursors = cursors;
			
			this.weight = weight;
			
			init();			
		}
		
		//METHODS...
		
		// initializing...
		private function init():void
		{
			
			view = new ResizablePaneView(target,weight);
			
			for(var i:int = 0;i < view.consolUIs.length && i < cursors.length;i++)
			{
				
				var button:MCButton = new MCButton(view.consolUIs[i],false);
							
				button.data = {cursor:cursors[i],type:ResizablePaneView.CONSOLS[i]};
				
				button.addEventListener(MouseEvent.MOUSE_OVER,buttonOverHandler);
				
				button.addEventListener(MouseEvent.MOUSE_OUT,buttonOutHandler);
				
				button.addEventListener(MouseEvent.MOUSE_DOWN,buttonDownHandler);
				
			}	
			
			
		}
		
		// on mouse down...
		private function buttonDownHandler(evt:MouseEvent):void
		{
			
			var button:MCButton = evt.currentTarget as MCButton;
			
			activeBar = button;
			
			activePoint = new Point(view.consolContainer.mouseX,view.consolContainer.mouseY);
			
			resizing = true;
			
			if(target.parent)
			oriRect = view.consolContainer.getBounds(target.parent);
						
			target.stage.addEventListener(MouseEvent.MOUSE_MOVE,stageMouseMoveHandler);
			
			target.stage.addEventListener(MouseEvent.MOUSE_UP,stageMouseUpHandler);
			
		}	
		
		// on stage mouse move...
		private function stageMouseMoveHandler(evt:MouseEvent):void
		{

			var type:String = activeBar.data["type"];
			
			if(type.indexOf("t") != -1 || type.indexOf("b") != -1)
			view.consolContainer.y =  target.mouseY - activePoint.y ;
			
			if(type.indexOf("l") != -1 || type.indexOf("r") != -1)
			view.consolContainer.x = target.mouseX - activePoint.x;
			
			var tlPoint:Point = new Point;
			
			var brPoint:Point = new Point;
			
			if(target.parent)
			{
				
				var consoleRect:Rectangle = view.consolContainer.getBounds(target.parent);	
				
				if(type.indexOf("l") != -1)
				{
					
					tlPoint.x = consoleRect.left;
					
					brPoint.x = oriRect.right;
					
					
				}else{
					
					tlPoint.x = oriRect.left;
					
					brPoint.x = consoleRect.right;
					
				}	
				
				if(type.indexOf("t") != -1)
				{
					
					tlPoint.y = consoleRect.top;
					
					brPoint.y = oriRect.bottom;
					
					
				}else{
					
					tlPoint.y = oriRect.top;
					
					brPoint.y = consoleRect.bottom;
					
					
				}	
				
				target.x = tlPoint.x;
				
				target.y = tlPoint.y;
				
				target.width = brPoint.x - tlPoint.x;
				
				target.height = brPoint.y - tlPoint.y;
				
				
			}	
			
		}	
		
		// on mouse up...
		private function stageMouseUpHandler(evt:MouseEvent):void
		{
			
			target.stage.removeEventListener(MouseEvent.MOUSE_MOVE,stageMouseMoveHandler);
			
			target.stage.removeEventListener(MouseEvent.MOUSE_UP,stageMouseUpHandler);
			
			resizing = false;
			
			update();

			dispatchEvent(new ResizePaneEvents(ResizePaneEvents.PANE_RESIZE));
			
		}	
		
		
		// on boerder button over...
		private function buttonOverHandler(evt:MouseEvent):void
		{
			
			var cursorClass:Class = evt.currentTarget.data["cursor"] as Class;
			
			CursorManager.setCursor(cursorClass);
			
		}	
		
		// on boerder button out...
		private function buttonOutHandler(evt:MouseEvent):void
		{
			
			CursorManager.removeAllCursors();
			
		}	
		
		// updae size...
		public function update():void
		{
			
			if(resizing) return;
			
			view.fitSizeToTarget();
			
		}	
		
		// set mouse enbled...
		public function set resizable(able:Boolean):void
		{
			
			view.consolContainer.mouseEnabled = able;
			
			view.consolContainer.mouseChildren = able;
			
		}
		
		//get mouse enabled...
		public function get resizable():Boolean
		{
			
			return view.consolContainer.mouseEnabled;
			
		}
		
	
		//END...
	}
}